
<%@ page pageEncoding="utf-8"%>
<%request.setCharacterEncoding("utf-8");%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%request.setCharacterEncoding("utf-8");%>
<%@ page import="com.ecomm.chat.manager.*"%>
<%@ page import="com.ecomm.chat.pojo.*"%>
<%@include file="/include/currentuserinfo.jsp"%>
<%@include file="/include/pageexpires.jsp"%>
<%@include file="/include/filepath.jsp"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <META HTTP-EQUIV="pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
    <META HTTP-EQUIV="expires" CONTENT="0">
	<link type="text/css" rel="stylesheet" href="<%=ctxpath%>/themes/newstyle/styles/office97/office97menu.css" id="menuStyleSheet">
    <title>易讯消息</title>
    <% 
    String groupid = (String)request.getParameter("groupid") != null ?(String)request.getParameter("groupid"):"";
    String nick = (String)request.getParameter("nick") != null ?(String)request.getParameter("nick"):"";
    String id = (String)request.getParameter("id") != null ?(String)request.getParameter("id"):"";
    String chatAction = "";
    
    ChatRemindManager chatrm = ChatRemindManager.getInstance();
    ChatMessageManager cmm = ChatMessageManager.getInstance();

    java.util.ArrayList arraylist = null;
    String parentid = (String)request.getParameter("parentid") != null ?(String)request.getParameter("parentid"):"";
    String type = (String)request.getParameter("type") != null ?(String)request.getParameter("type"):"";
    //String chatid = (String)request.getParameter("chatid") != null ?(String)request.getParameter("chatid"):"";
      String bigtype = (String)request.getParameter("bigtype") != null ?(String)request.getParameter("bigtype"):"2";
    if(groupid != null && !groupid.equals("")){
      chatAction = groupid;
    }else if(parentid != null && !parentid.equals("")){
      chatAction = parentid;
    }else{
      chatAction =ChatListenManager.getInstance().chatListen(nick,id);
    }
    if(parentid != null && !parentid.equals("")){
      arraylist = (java.util.ArrayList)cmm.getChatmessageForNew(nick,parentid,id,bigtype);
    }
    //new add by ppliang 5.18
    %>
    
    <script type="text/javascript" src="<%=ctxpath%>/chat/js/util.js"></script>
    
    <script type="text/javascript" src="<%=ctxpath%>/chat/js/ajax-pushlet-client.js"></script>
    
    <script type="text/javascript"><!--
      var chatDoc;
      var chatFrame;
      var nick=getPageParameter('nick', 'anon');
      var nickChineseName = "<%=username%>";
      window.onload = chat_init;

function chat_init(){
        if (window.frames && window.frames["chatContents"]) //IE 5 (Win/Mac), Konqueror, Safari
          chatFrame = window.frames["chatContents"];
        else if (document.getElementById("chatContents").contentWindow) //IE 5.5+, Mozilla 0.9+, Opera
          chatFrame = document.getElementById("chatContents").contentWindow;
        else //Moz < 0.9 (Netscape 6.0)
          chatFrame = document.getElementById("chatContents");

        if(chatFrame.document) //Moz 0.9+, Konq, Safari, IE, Opera
          chatDoc = chatFrame.document;
        else //Moz < 0.9 (Netscape 6.0)
          chatDoc = chatFrame.contentDocument;
          
           enterChat();
               
       document.forms[0].msg.focus();
       }

 		// Event Callback for join
function onJoinAck(event) {
	 		appendMessage('欢迎你进入易讯聊天系统！');
         <%
    if(arraylist != null && arraylist.size() >0){
      for(int k = 0;k< arraylist.size() ;k++){
        Object[] object = (Object[])arraylist.get(k);
        %>
        appendMessage('<%=com.ecomm.util.CommonMethod.getUserNameByUserID(object[0].toString())%>:<%=object[1].toString()%><br><%=object[2].toString()%>');
        <%
      }
    }
                        %>
		}

       // Event Callback: display all events
function onData(event) {
	var myDate = new Date();
        var action = event.get('action');
        var nickid = event.get('nick');
         var req=new XMLHttpRequest(); 
       if (req) {  
          req.onreadystatechange=function() {  
           if (req.readyState==4 && req.status==200) {//判断状态，4是已发送，200已完成  
             var  nickName = req.responseText;
          if (nickid != '<%=userid%>'){
          if (action == 'send') { //改为自己的名字，从USER去取 
          appendMessage('<b>' + nickName + '</b>&nbsp;&nbsp;&nbsp;'+ myDate.toLocaleTimeString()+' <br> <i>' + event.get('msg') + '</i>');
          //document.getElementById("ChatSound").innerHTML ="<html><head><title>uecomm</title></head><body><MARQUEE scrollAmount=6 scrollDelay=110 behavior=scroll direction=left><embed src=\"/chat/config/2.mid\" hidden=\"true\" LOOP=\"false\" autostart=\"true\" /></MARQUEE></body></html>";
          } else if (action == 'enter') {
               appendMessage('<b><i>*** ' + nickName + ' 加入聊天  ***</i></b>');
          } else if (action == 'exit') {
             appendMessage('<b><i>*** ' + nickName + ' 退出聊天  ***</i></b>');
         }
         }
         }  
         }
       req.open('post', '<%=ctxpath%>/chat/ajaxcheck.do?method=getName&id='+nickid);
       req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
       req.setRequestHeader("Content-Type","text/html");  
       req.setRequestHeader("Content-Type","utf-8");
       req.send("");//发送参数如果有参数req.send("username="+user_name);用request取得  
      }  
      }

    // Event Callback: display all events
	function onNack(event) {
	   alert('negative response from server: ' + event.getEvent() + ' reason: ' + event.get('p_reason'));
    }
      function appendMessage(content){
        var newDiv = chatDoc.createElement("DIV");
        newDiv.innerHTML = content;
       
        chatDoc.getElementById("contents").appendChild(newDiv);
        chatFrame.scrollTo(0, chatDoc.getElementById("contents").offsetHeight);

      }
      function enterChat(){
        p_load();
        p_join_listen('<%=chatAction%>');
       }
  function onListenAck(event){
      p_publish('<%=chatAction%>', 'action', 'enter', 'nick', nick);
     //  alert("listen.....");
   }
        function sendMsg(){
        if(document.getElementById("msg").value ==''){
           document.forms[0].msg.focus();
           return false;
        }
        var myDate = new Date();
        appendMessage( '<b>' +nickChineseName + '</b>&nbsp;&nbsp;&nbsp;'+ myDate.toLocaleTimeString()+' <br> <i>' +  document.getElementById("msg").value + '</i>');
        p_publish('<%=chatAction%>', 'action', 'send', 'nick', nick, 'msg', document.getElementById("msg").value);
        resetForm();
       }

      function leaveChat(){
 
          p_publish('<%=chatAction%>', 'action', 'exit', 'nick', nick);
          p_leave();
       //删除 单聊新加入的人员
        var url = '<%=ctxpath%>/chat/ajaxcheck.do?method=deleteListenJionName&id=<%=chatAction%>';
        httpRequestleave(url);
        window.parent.close()
     }

     function gotoEnter() {
        //window.location.href='enter.jsp';
     }
    function todo(){
      if(window.event.ctrlKey&&(window.event.keyCode==13)) {//ctrl+enter 同时按这2个键
    //  if(event.keyCode ==13){//enter 
              sendMsg();
      }
  }
  function leave_chat(){
  //删除 单聊新加入的人员
  p_publish('<%=chatAction%>', 'action', 'exit', 'nick', nick);
  var url = '<%=ctxpath%>/chat/ajaxcheck.do?method=deleteListenJionName&id=<%=chatAction%>';
  httpRequestleave(url);
   //更新参加聊天的人数
   //url = '<%=ctxpath%>/chat/ajaxcheck.do?method=updateJionPushlet&id=<%=chatAction%>';
   //httpRequestleave(url);
  }
    function httpRequestleave(url)
    {
          var req;
     if (window.XMLHttpRequest) {
         req = new XMLHttpRequest();
     }else if (window.ActiveXObject) {
         req = new ActiveXObject("Microsoft.XMLHTTP");
     }
    if(req){
        req.open("GET", url, true);
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.setRequestHeader("Content-Type","text/html"); 
        req.setRequestHeader("Content-Type","utf-8");
        req.send(null);  
     }	
    }
//onbeforeunload="leaveChat()"
     function resetForm(){
        document.forms[0].msg.value = "";
        document.forms[0].msg.focus();
     }//-->
    </script>
  </head>
  <body onbeforeunload="leave_chat()" id="chatleftbody">	
    <div id="ChatSound" style="display:none"></div>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="chat_left_table">
      <tr>
        <td class="chat_left_toptd"><iframe id="chatContents" name="chatContents" src="contents.html" width="100%" height="100%" frameborder="0" ></iframe></td>
      </tr>
      <tr>
        <td class="chat_left_bottomtd"><form>
          <table width="100%" border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td class="chat_textareatd"><textarea name="msg" id="msg" rows="8" cols="28"  onkeydown=todo()></textarea>
              </td>
              <td class="centerbuttontd"><input name="button" type="button" title="快捷键：[Ctrl+Enter]" onClick="javascript:sendMsg()" value="发送" class="button">
                  <br>
                [Ctrl+Enter]
                <!--  <input type="button" value="退出" title="按退出会减小负载" onclick="javascript:leaveChat()">-->
              </td>
            </tr>
          </table>
        </form>
        <%
		
	   // if(chatid != null && !chatid.equals("")){ //有工作流的消息
	   //   chatrm.updateRemindStateWorkflow(chatid,"1");
	  //  }
		if(parentid != null && !parentid.equals("")){
		  chatrm.updateRemindState(id,"1",parentid,nick,type,bigtype);
		}
		%>
		</td>
      </tr>
    </table>
</body>
</html>
